<?php
namespace app\admin\service;

use app\model\AuthGroup;
use app\model\AuthRule;
use app\model\Menu;

/**
 * Class MenuService
 * @package app\service
 */
class MenuService extends CommonService
{
    /**
     * 获取菜单列表
     * @param $request
     * @return array
     */
    public static function getList($request = [])
    {
        $menuList = AuthRule::getList([], '', 'sort DESC, id ASC');
        foreach ($menuList as &$item)
        {
            if (!empty($request['showType']))
            {
                $item['open'] = 1;  // 全部展开
            } else {
                $item['open'] = 0;  // 全部折叠
            }
        }
        return $menuList;
    }

    /**
     * 获取菜单树状列表
     * @return array
     */
    public static function getTreeList()
    {
        $menuList = AuthRule::getList(['status' => 1], '', 'sort DESC, id ASC');
        $treeList = array_values(listToTree($menuList));
        return $treeList;
    }

    /**
     * 获取用户权限菜单
     * @param $admin_id
     */
    public static function getAdminTree($admin)
    {
        $result = $list = [];

        // 所有的菜单
        $menus = AuthRule::getList(['status' => 1], '', 'sort DESC, id ASC');
        $menuList = self::menuHref($menus);
        if ($admin['is_admin'] == 1)
        {
            // 超级管理员全部菜单
            $result = array_values(listToTree($menuList));
        } else {
            // 获取分组身份
            $adminAuthGroup = AuthGroup::getAdminGroup($admin['id']);
            $rules = explode(',', $adminAuthGroup['rules']);
            foreach($menuList as $item)
            {
                if (in_array($item['id'], $rules) || $item['is_control'] == 0)
                {
                    $list[] = $item;
                }
            }
            $result = array_values(listToTree($list));
        }

        return $result;
    }

    /**
     * 处理菜单的链接
     */
    public static function menuHref($list)
    {
        foreach($list as &$item)
        {
            if ($item['addon'] == 'admin')
            {
                $item['href'] = '/admin/' . $item['url'];
            } else {
                $item['href'] = '/' . $item['addon'] . '/admin/' . $item['url'];
            }
        }
        return $list;
    }

    /**
     * 获取当前menu
     * @param $id
     * @return array
     */
    public static function getMenu($id)
    {
        return AuthRule::getById($id);
    }

    /**
     * 编辑菜单
     */
    public static function edit($data)
    {
        // 菜单等级处理
        if (!empty($data['pid']))
        {
            $data['lv'] = self::getLevel($data['pid']);
        }

        if ($data['id'])
        {
            AuthRule::updateOne($data, ['id' => $data['id']]);
        } else {
            AuthRule::addOne($data);
        }
        return true;
    }

    /**
     * 删除菜单
     * @param $ids
     * @return bool
     */
    public static function del($ids)
    {
        return AuthRule::deleteByIds($ids);
    }

    /**
     * 获取当前页面的同级四级菜单
     * @param $url
     * @return array
     */
    public static function getFourthMenu($url)
    {
        // 解析URL
        $params = array_values(array_filter(explode('/', $url)));
        if (empty($params))
        {
            return [];
        }

        // 解析路径
        $addon = $params[0];
        if ($addon == 'admin')
        {
            $url = $params[1] . '/' . $params[2];
        } else {
            $url = $params[2] . '/' . $params[3];
        }

        // 获取当前路径的菜单
        $thisMenu = AuthRule::getOne(['addon' => $addon, 'url' => $url, 'lv' => 4]);

        $menuList = [];
        if (!empty($thisMenu))
        {
            $menuList = self::menuHref(AuthRule::getList(['pid' => $thisMenu['pid']]));
            foreach ($menuList as &$item)
            {
                // 检查是否高亮
                $item['checked'] = ($item['id'] == $thisMenu['id']) ? true : false;
            }
        }


        return $menuList;
    }

    /**
     * 获取菜单等级
     * @param $pid
     */
    private static function getLevel($pid)
    {
        if ($pid == 0)
        {
            return 1;
        }
        $pmenu = AuthRule::getById($pid);
        if ($pmenu['pid'] == 0)
        {
            return 2;
        }
        $ppmenu = AuthRule::getById($pmenu['pid']);
        if ($ppmenu['pid'] == 0)
        {
            return 3;
        }
        return 4;
    }
}